package com.shaodongbo.gson;

import android.support.constraint.solver.GoalRow;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private String animalStr = "{\n" +
            "\t\"animals\":{\n" +
            "\t\"dog\":[\n" +
            "\t\t{\"name\":\"Rufus\",\"breed\":\"labrador\",\"count\":1,\"twoFeet\":false},\n" +
            "\t\t{\"name\":\"Marty\",\"breed\":\"whippet\",\"count\":1,\"twoFeet\":false}\n" +
            "\t],\n" +
            "\t\"cat\":{\"name\":\"Matilda\"}\n" +
            "}\n" +
            "}";
    private String weatherStr = "{\"time\":\"2019-06-20 19:12:23\",\"cityInfo\":{\"city\":\"广州市\",\"cityId\":\"101280101\",\"parent\":\"广东\",\"updateTime\":\"18:00\"},\"date\":\"20190620\",\"message\":\"Success !\",\"status\":200,\"data\":{\"shidu\":\"68%\",\"pm25\":8.0,\"pm10\":23.0,\"quality\":\"优\",\"wendu\":\"32\",\"ganmao\":\"各类人群可自由活动\",\"yesterday\":{\"date\":\"19\",\"sunrise\":\"05:41\",\"high\":\"高温 34.0℃\",\"low\":\"低温 27.0℃\",\"sunset\":\"19:15\",\"aqi\":27.0,\"ymd\":\"2019-06-19\",\"week\":\"星期三\",\"fx\":\"南风\",\"fl\":\"3-4级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},\"forecast\":[{\"date\":\"20\",\"sunrise\":\"05:41\",\"high\":\"高温 34.0℃\",\"low\":\"低温 28.0℃\",\"sunset\":\"19:15\",\"aqi\":23.0,\"ymd\":\"2019-06-20\",\"week\":\"星期四\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"雷阵雨\",\"notice\":\"带好雨具，别在树下躲雨\"},{\"date\":\"21\",\"sunrise\":\"05:42\",\"high\":\"高温 35.0℃\",\"low\":\"低温 28.0℃\",\"sunset\":\"19:15\",\"aqi\":115.0,\"ymd\":\"2019-06-21\",\"week\":\"星期五\",\"fx\":\"西南风\",\"fl\":\"3-4级\",\"type\":\"雷阵雨\",\"notice\":\"带好雨具，别在树下躲雨\"},{\"date\":\"22\",\"sunrise\":\"05:42\",\"high\":\"高温 35.0℃\",\"low\":\"低温 27.0℃\",\"sunset\":\"19:15\",\"aqi\":124.0,\"ymd\":\"2019-06-22\",\"week\":\"星期六\",\"fx\":\"西南风\",\"fl\":\"3-4级\",\"type\":\"雷阵雨\",\"notice\":\"带好雨具，别在树下躲雨\"},{\"date\":\"23\",\"sunrise\":\"05:42\",\"high\":\"高温 34.0℃\",\"low\":\"低温 27.0℃\",\"sunset\":\"19:15\",\"aqi\":125.0,\"ymd\":\"2019-06-23\",\"week\":\"星期日\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"雷阵雨\",\"notice\":\"带好雨具，别在树下躲雨\"},{\"date\":\"24\",\"sunrise\":\"05:42\",\"high\":\"高温 33.0℃\",\"low\":\"低温 27.0℃\",\"sunset\":\"19:16\",\"aqi\":121.0,\"ymd\":\"2019-06-24\",\"week\":\"星期一\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"雷阵雨\",\"notice\":\"带好雨具，别在树下躲雨\"},{\"date\":\"25\",\"sunrise\":\"05:42\",\"high\":\"高温 33.0℃\",\"low\":\"低温 27.0℃\",\"sunset\":\"19:16\",\"aqi\":116.0,\"ymd\":\"2019-06-25\",\"week\":\"星期二\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"雷阵雨\",\"notice\":\"带好雨具，别在树下躲雨\"},{\"date\":\"26\",\"sunrise\":\"05:43\",\"high\":\"高温 33.0℃\",\"low\":\"低温 27.0℃\",\"sunset\":\"19:16\",\"ymd\":\"2019-06-26\",\"week\":\"星期三\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"雷阵雨\",\"notice\":\"带好雨具，别在树下躲雨\"},{\"date\":\"27\",\"sunrise\":\"05:43\",\"high\":\"高温 33.0℃\",\"low\":\"低温 26.0℃\",\"sunset\":\"19:16\",\"ymd\":\"2019-06-27\",\"week\":\"星期四\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"28\",\"sunrise\":\"05:43\",\"high\":\"高温 33.0℃\",\"low\":\"低温 27.0℃\",\"sunset\":\"19:16\",\"ymd\":\"2019-06-28\",\"week\":\"星期五\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"29\",\"sunrise\":\"05:44\",\"high\":\"高温 33.0℃\",\"low\":\"低温 27.0℃\",\"sunset\":\"19:16\",\"ymd\":\"2019-06-29\",\"week\":\"星期六\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"30\",\"sunrise\":\"05:44\",\"high\":\"高温 35.0℃\",\"low\":\"低温 28.0℃\",\"sunset\":\"19:16\",\"ymd\":\"2019-06-30\",\"week\":\"星期日\",\"fx\":\"西南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"01\",\"sunrise\":\"05:44\",\"high\":\"高温 35.0℃\",\"low\":\"低温 27.0℃\",\"sunset\":\"19:16\",\"ymd\":\"2019-07-01\",\"week\":\"星期一\",\"fx\":\"西北风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"02\",\"sunrise\":\"05:44\",\"high\":\"高温 35.0℃\",\"low\":\"低温 26.0℃\",\"sunset\":\"19:17\",\"ymd\":\"2019-07-02\",\"week\":\"星期二\",\"fx\":\"北风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"03\",\"sunrise\":\"05:45\",\"high\":\"高温 35.0℃\",\"low\":\"低温 26.0℃\",\"sunset\":\"19:17\",\"ymd\":\"2019-07-03\",\"week\":\"星期三\",\"fx\":\"东北风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"04\",\"sunrise\":\"05:45\",\"high\":\"高温 35.0℃\",\"low\":\"低温 26.0℃\",\"sunset\":\"19:17\",\"ymd\":\"2019-07-04\",\"week\":\"星期四\",\"fx\":\"东北风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"}]}}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                String str1 = gson.toJson(1234);
                String str2 = gson.toJson("Hello");
                String str3 = gson.toJson(false);
                String[] arr = {"Hello", "World", "Welcome"};
                String str4 = gson.toJson(arr);

                Log.e(TAG, "str1 = " + str1 + " str2 = " + str2 + " str3 = " + str3 + " str4 = " + str4);
            }
        });

        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student student = new Student("张三", 20);
                Gson gson = new Gson();
                String str = gson.toJson(student);
                Log.e(TAG, "str = " + str);

                //  JSONObject转Json
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("name", student.getName());
                    jsonObject.put("age", student.getAge());
                    Log.e(TAG, "jsonObject = " + jsonObject.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = "{\"name\":\"张三\",\"age\":20}";
                Gson gson = new Gson();
                Student student = gson.fromJson(str, Student.class);
                Log.e(TAG, "student : " + student);
            }
        });

        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                JsonRootBean jsonRootBean = gson.fromJson(animalStr, JsonRootBean.class);
                String dogName = jsonRootBean.getAnimals().getDog().get(0).getName();
                String catName = jsonRootBean.getAnimals().getCat().getName();
                Log.e(TAG, "dog : " + dogName + " cat : " + catName);
            }
        });

        //  使用GsonFormat生成了AnimalData类型
        findViewById(R.id.btn5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                AnimalData animalData = gson.fromJson(animalStr, AnimalData.class);
                String dogName = animalData.getAnimals().getDog().get(0).getName();
                String catName = animalData.getAnimals().getCat().getName();
                Log.e(TAG, "dog : " + dogName + " cat : " + catName);
            }
        });

        findViewById(R.id.btn6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                Weather weather = gson.fromJson(weatherStr, Weather.class);
                String type = weather.getData().getForecast().get(0).getType();
                String low = weather.getData().getForecast().get(0).getLow();
                String high = weather.getData().getForecast().get(0).getHigh();
                Log.e(TAG, "天气 ：" + type + " 低温 ：" + low + " 高温 ：" + high);
            }
        });
    }
}
